﻿using AesonBaseFramework.EntityFramework.BaseEntities;
using AesonIntegrationTestLibrary.PubSubEvents;
using Microsoft.Practices.Prism.PubSubEvents;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace AesonIntegrationTestLibrary.DataModel
{
    public class AesonTestFlow : AesonBaseEntity
    {
        #region Private Fields

        private TimeSpan _duration;
        private DateTime _endDate;
        private string _message;
        private DateTime _startDate;
        private int _testFailed;
        private string _testGuid;
        private int _testNumber;
        private int _testPassed;
        private List<AesonTest> _testResults;
        private int _totalTest;

        #endregion Private Fields

        #region Public Properties

        public TimeSpan Duration
        {
            get { return _duration; }
            set
            {
                if (value == _duration) return;
                _duration = value;
                OnPropertyChanged();
            }
        }

        public DateTime EndDate
        {
            get { return EndDate; }
            set
            {
                if (value == EndDate) return;
                EndDate = value;
                OnPropertyChanged();
            }
        }

        public IEventAggregator ev { get; set; }

        public string Message
        {
            get { return _message; }
            set
            {
                if (value == _message) return;
                _message = value;
                OnPropertyChanged();
            }
        }

        public DateTime StartDate
        {
            get { return _startDate; }
            set
            {
                if (value == _startDate) return;
                _startDate = value;
                OnPropertyChanged();
            }
        }

        public int TestFailed
        {
            get { return _testFailed; }
            set
            {
                if (value == _testFailed) return;
                _testFailed = value;
                OnPropertyChanged();
            }
        }

        public string TestGuid
        {
            get { return _testGuid; }
            set
            {
                if (value == _testGuid) return;
                _testGuid = value;
                OnPropertyChanged();
            }
        }

        public int TestNumber
        {
            get { return _testNumber; }
            set
            {
                if (value == _testNumber) return;
                _testNumber = value;
                OnPropertyChanged();
            }
        }

        public int TestPassed
        {
            get { return _testPassed; }
            set
            {
                if (value == _testPassed) return;
                _testPassed = value;
                OnPropertyChanged();
            }
        }
       
        public List<AesonTest> TestResults
        {
            get { return _testResults; }
            set
            {
                if (value == _testResults) return;
                _testResults = value;
                OnPropertyChanged();
            }
        }

        public int TotalTest
        {
            get { return _totalTest; }
            set
            {
                if (value == _totalTest) return;
                _totalTest = value;
                OnPropertyChanged();
            }
        }

        #endregion Public Properties

        #region Public Methods

        public static AesonTestFlow Create(IEventAggregator ea, int testNumber)
        {
            var t = new AesonTestFlow
            {
                TestGuid = Guid.NewGuid().ToString(),
                StartDate = DateTime.Now,
                TestResults = new List<AesonTest>(),
                TestNumber = testNumber
            };
            ea.GetEvent<AesonTestFlowStartedEvent>().Publish(t);
            return t;
        }

        public void AddTest(AesonTest t)
        {
            TotalTest++;
            if (t.Statut == TestStatut.Passed)
            {
                TestPassed++;
            }
            if (t.Statut == TestStatut.Failed)
            {
                TestFailed++;
            }
        }

        public string EndTest()
        {
            return "TestResult";
        }

        #endregion Public Methods
    }
}